package com.finance.expensrem;

import android.content.*;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import android.view.View.OnClickListener;

public class KeypadAdapter extends BaseAdapter {
	
	private Context mContext;
	
	// Declare button click listener variable
	private OnClickListener mOnButtonClick;

	public KeypadAdapter(Context mContext) {
		super();
		this.mContext = mContext;
	}
	
	// Method to set button click listener variable
	public void setOnButtonClickListener(OnClickListener Listener) {
		mOnButtonClick = Listener;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mButtons.length;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mButtons[position];
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Button btn;
		
		if (convertView == null) { // if it's not recycled, initialize some attributes
			
			btn = new Button(mContext);
			Keypad keypadButton = mButtons[position];
			
			switch(keypadButton.mCategory)
			{
				case CLEAR:
					//btn.setBackgroundResource(R.drawable.keypadclear1);
					break;
				case NUMBER:
					//btn.setBackgroundResource(R.drawable.keypad1);
					break;
				case OPERATOR:
					//btn.setBackgroundResource(R.drawable.keypadop1);
					break;
				case OTHER:
					//btn.setBackgroundResource(R.drawable.keypadother1);
					break;
				case RESULT:
					//btn.setBackgroundResource(R.drawable.keypadresult1);
					break;
				case DUMMY:
					//btn.setBackgroundResource(R.drawable.appvertical1);
					break;
				default:
					//btn.setBackgroundResource(R.drawable.keypad1);
					break;
			}
			
			// Set OnClickListener of the button to mOnButtonClick
			if(keypadButton != Keypad.DUMMY)
				btn.setOnClickListener(mOnButtonClick);
			else
				btn.setClickable(false);
			
			// Set CalculatorButton enumeration as tag of the button so that we
			// will use this information from our main view to identify what to do
			btn.setTag(keypadButton);
		}
		else
		{
			btn = (Button) convertView;
		}
		
		btn.setText(mButtons[position].getText());
		return btn;
	}
	
	// Create and populate keypad buttons array with CalculatorButton enum values
	private Keypad[] mButtons = {
			Keypad.CE,
			Keypad.C,
			Keypad.BACKSPACE,
			Keypad.DIV,
			
			Keypad.SEVEN,
			Keypad.EIGHT,
			Keypad.NINE,
			Keypad.MULTIPLY,
			
			Keypad.FOUR,
			Keypad.FIVE,
			Keypad.SIX,
			Keypad.MINUS,
			
			Keypad.ONE,
			Keypad.TWO,
			Keypad.THREE,
			Keypad.PLUS,
			
			Keypad.DUMMY,
			Keypad.ZERO,			
			Keypad.DOT,
			Keypad.RESULT
	};

}
